$(document).ready(function(){
	//extends datagrid for inlineediting
	$.extend($.fn.datagrid.methods, {
		editCell: function(jq,param){
			return jq.each(function(){
				var opts = $(this).datagrid('options');
				var fields = $(this).datagrid('getColumnFields',true).concat($(this).datagrid('getColumnFields'));
				for(var i=0; i<fields.length; i++){
					var col = $(this).datagrid('getColumnOption', fields[i]);
					col.editor1 = col.editor;
					if (fields[i] != param.field){
						col.editor = null;
					}
				}
				$(this).datagrid('beginEdit', param.index);
				for(var i=0; i<fields.length; i++){
					var col = $(this).datagrid('getColumnOption', fields[i]);
					col.editor = col.editor1;
				}
			});
		}
	});
});


/**
 * reloads a grid by given parameters and the grid id
 * @param 		string		gridId
 * @param 		mixed		params
 */
function reloadGrid(gridId, params, callback){
	gridId = getJqueryId(gridId);
	params = getEncodedParams(params);
	$(gridId).datagrid('loading');
	$.ajax({
		url: pageSettings.ajaxUrl,
		dataType: 'html',
		data: params,
		type : 'get'
	}).success(function(response) {
		var innerTable = response.substring(response.indexOf('>')+1, response.length-8);	//get the innerHtml from the response table
		clearGrid(gridId);	
		$(gridId).html(innerTable);		//replace the innerhtml from response with the content from the existing table
		$(gridId).datagrid(); 
		$('.easyui-tooltip').tooltip();

		if (typeof callback == 'function')
			callback(gridId);
	});
}


/**
 * returns the sort order for given grid and sort column
 * @param 		string		grid
 * @param 		string		column
 * @returns		string		order
 */
function getOrder(grid, column){
	initGridSort(grid);	
	var lastOrder = (pageSettings.grids[grid].sortColumn[column] == null) ? 'asc' : pageSettings.grids[grid].sortColumn[column];
	order = (lastOrder == 'asc') ? 'desc' : 'asc';
	pageSettings.grids[grid].sortColumn[column] = order;

	return order;
}


/**
 * initializes the pageSettings.grids by given grid
 * @param 		string		grid
 */
function initGridSort(grid){
	if (pageSettings.grids[grid] == null){
		pageSettings.grids[grid] = {};
		pageSettings.grids[grid].sortColumn = {};
	}
}


/**
 * removes all rows in the datagrid
 * @param 		string		gridId
 */
function clearGrid(gridId){
	$(gridId).datagrid('loadData', {"total":0,"rows":[]});	
}


/**
 * handles the sorting of the grid
 * @param 		string		column
 * @param 		string		order
 */
function mainGridSort(column, order){
	order = getOrder('mainGrid', column);
	var jsonInfo = getCheckedJsonInfo('filtertree');
	var filter = getFilterText();
	var params = {'T':'FILTER_GRID', 'FILTER':filter, 'SORT':column, 'ORDER':order, 'GRID':'MAIN', 'CHECKED_TREE_ITEMS':jsonInfo};
	reloadGrid('mainGrid', params);
}


/**
 * styles the cell
 * @param 		string		value
 * @param 		mixed		row
 * @param 		int			index
 * @returns 	string
 */
function positiveStyler(value,row,index){
	return 'background-color:#DAFFD9; color:#047F00;';
}


/**
 * returns the ids of all checked rows
 * @param 		string		gridId
 * @returns		array
 */
function getCheckedGridIds(gridId){
	return getGridIds(gridId, 'getChecked');
}


/**
 * returns the ids of the rows which are selected by the action
 * @param 		string		gridId
 * @param 		string		action
 * @returns 	array
 */
function getGridIds(gridId, action){
	gridId = getJqueryId(gridId);
	var rows = $(gridId).datagrid(action);
	var ids = [];

	for(var rowId in rows){
		var row = rows[rowId];
		if (row.id != null && row.id != '')
			ids.push(row.id);
	}

	return ids;
}


/**
 * adapts the height of the grid to the its rows
 * @param 		string		gridId
 */
function adaptGridHeight(gridId){
	var gridId = getJqueryId(gridId);
	var rows = $(gridId).datagrid('getRows');
	$(gridId).datagrid('resize', {'height': ((rows.length * 28) + 50)});	//28 is the height of one line in px (ca.).
	//add 50 px. this included the header row and short place after grid rows
}


/**
 * remove all selected rows of the grid
 * @param 		int		gridId
 */
function removeCheckedRows(gridId){
	var rowIds = getCheckedGridIds(gridId);
	var gridId = getJqueryId(gridId);
	var index = null;

	for(var row in rowIds){
		rowId = rowIds[row];
		index = $(gridId).datagrid('getRowIndex', rowId);
		$(gridId).datagrid('deleteRow', index);
	}
}


/**
 * returns all ids of the rows
 * @param 		string		gridId
 * @returns		array
 */
function getAllGridIds(gridId){
	return getGridIds(gridId, 'getRows');
}


/**
 * returns the information of all rows
 * @param 		string		gridId
 * @returns		mixed		rowInfo
 */
function getAllRowInformation(gridId){
	gridId = getJqueryId(gridId);
	var columns = $(gridId).datagrid('getColumnFields');
	var rows = $(gridId).datagrid('getRows');
	var rowInfo = {};

	for(var rowId in rows){
		var row = rows[rowId];
		if (row.id != null && row.id != ''){
			rowInfo[row.id] = {}
			for(var columnId in columns){
				var column = columns[columnId];
				rowInfo[row.id][column.toUpperCase()] = row[column];
			}
		}
	}
	return rowInfo;
}